package com.crane.common.core.utils;


import java.util.regex.Pattern;

/**
 * 正则校验
 */
public class Validator {
    /**
     * 正则表达式：验证用户名
     */
    public static final String REGEX_USERNAME = "|(?:^1[3|4|5|6|7|8|9]\\d{9}$)";
    public static final String REGEX_USERNAME_MSG = "用户名格式错误";

    /**
     * 正则表达式：验证密码
     */
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";
    public static final String REGEX_PASSWORD_MSG = "密码格式错误";

    /**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_MOBILE = "|(?:^1[3|4|5|6|7|8|9]\\d{9}$)";
    public static final String REGEX_MOBILE_MSG = "手机号格式错误";

    /**
     * 正则表达式：验证邮箱
     */
    public static final String REGEX_EMAIL = "|(?:^([a-z0-9A-Z]+[-_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$)";
    public static final String REGEX_EMAIL_MSG = "邮箱格式错误";
    /**
     * 正则表达式：验证汉字
     */
    public static final String REGEX_CHINESE = "|(?:^[\u4e00-\u9fa5],{0,}$)";

    /**
     * 正则表达式：验证身份证
     */
    public static final String REGEX_ID_CARD = "|(?:^([1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X|x))$)";
    public static final String REGEX_ID_CARD_MSG = "身份证格式错误";

    /**
     * 正则表达式：验证URL
     */
    public static final String REGEX_URL = "|(?:http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?)";
    public static final String REGEX_URL_CARD_MSG = "url 格式错误";

    /**
     * 正则表达式：验证IP地址
     */
    public static final String REGEX_IP_ADDR = "|(?:(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d))";
    public static final String REGEX_IP_ADDR_MSG = "IP地址格式错误";
    /**
     * 正则表达式：验证邮政编码
     */
    public static final String REGEX_ZIP_CODE = "|(?:(^[1-9][0-9]{5}$))";
    public static final String REGEX_ZIP_CODE_MSG = "邮政编码格式错误";


    public static final String REGEX_CHINESENAME = "^([a-zA-Z0-9\\u4e00-\\u9fa5\\·]{1,10})$";
    public static final String REGEX_CHINESENAME_MSG = "姓名格式错误";


    /**
     * 检验中文字符
     *
     * @param name
     * @return
     */
    public static boolean isChineseName(String name) {
        return Pattern.matches(REGEX_CHINESENAME, name);
    }

    /**
     * 校验用户名
     *
     * @param username
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isUsername(String username) {
        return Pattern.matches(REGEX_USERNAME, username);
    }

    /**
     * 校验密码
     *
     * @param password
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isPassword(String password) {
        return Pattern.matches(REGEX_PASSWORD, password);
    }

    /**
     * 校验手机号
     *
     * @param mobile
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isMobile(String mobile) {
        return Pattern.matches(REGEX_MOBILE, mobile);
    }

    /**
     * 校验邮箱
     *
     * @param email
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isEmail(String email) {
        return Pattern.matches(REGEX_EMAIL, email);
    }

    /**
     * 校验汉字
     *
     * @param chinese
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isChinese(String chinese) {
        return Pattern.matches(REGEX_CHINESE, chinese);
    }

    /**
     * 校验身份证
     *
     * @param idCard
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isIDCard(String idCard) {
        return Pattern.matches(REGEX_ID_CARD, idCard);
    }

    /**
     * 校验URL
     *
     * @param url
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isUrl(String url) {
        return Pattern.matches(REGEX_URL, url);
    }

    /**
     * 校验IP地址
     *
     * @param ipAddr
     * @return
     */
    public static boolean isIPAddr(String ipAddr) {
        return Pattern.matches(REGEX_IP_ADDR, ipAddr);
    }

    /**
     * 校验邮政编码
     *
     * @param zipCode
     * @return
     */
    public static boolean isZipCode(String zipCode) {
        return Pattern.matches(REGEX_ZIP_CODE, zipCode);
    }


}
